//
//  MCTraningVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/17.
//

import UIKit
import QMUIKit
import SnapKit
import Toast_Swift
import MJRefresh
import Kingfisher


class MCTraningVc: MCBaseVc {

    let mcTraningTableIdentifier = "mcTraningTableIdentifier"
    
    public var instrumentId:String = ""
    
    var allTraningTableView:UITableView!
    
    private var allTraningDataSource:Array = [MCIntrumentRecommandRecord?]()
  
    private var current:Int = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "全部动作"
    
        
        allTraningTableView = UITableView.init(frame: CGRect.zero, style: .plain)
        allTraningTableView.dataSource = self
        allTraningTableView.delegate = self
        allTraningTableView.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.view.addSubview(allTraningTableView)
        allTraningTableView.snp.makeConstraints { make in
            make.left.equalTo(self.view.snp.left).offset(0.0)
            make.top.equalTo(self.view.snp.top).offset(0.0)
            make.right.equalTo(self.view.snp.right).offset(0.0)
            make.bottom.equalTo(self.view.snp.bottom).offset(0.0)
        }
        
        
        self.allTraningTableView.register(UINib(nibName: "MCTraningTableCell", bundle: Bundle.main), forCellReuseIdentifier: mcTraningTableIdentifier)
        
        
        self.allTraningTableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.current = 1
            self.getRecommandAction(current: self.current, instruments: self.instrumentId)
            
        })
        
        self.allTraningTableView.mj_header?.beginRefreshing()
        
        
        self.allTraningTableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            self.getRecommandAction(current: self.current, instruments: self.instrumentId)
        })
        
        
        
    }
    
    func getRecommandAction(current:Int?,instruments:String) -> Void {
        NetWorkRequest(MCActionDetailApi.getInstrumentInfoActionDetail(current:current ?? 0, instrumentIds: instruments)) { ResponseModel in
            let recommandList = MCIntrumentRecommandListModel.init(JSONString: ResponseModel.dataString)
            
            self.allTraningTableView.mj_footer?.endRefreshing()
            self.allTraningTableView.mj_header?.endRefreshing()
            if current == 1 {
                self.allTraningDataSource.append(contentsOf: recommandList?.records ?? [])
            }else{
                self.current += 1
                self.allTraningDataSource.append(contentsOf: recommandList?.records ?? [])
            }
            
            self.allTraningTableView.reloadData()
            
        }failureCallback: { ResponseModel in
            self.allTraningTableView.mj_footer?.endRefreshing()
            self.allTraningTableView.mj_header?.endRefreshing()
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    
}

extension MCTraningVc:UITableViewDataSource,UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.allTraningDataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MCTraningTableCell = tableView.dequeueReusableCell(withIdentifier: mcTraningTableIdentifier,for: indexPath) as! MCTraningTableCell
        
        let allTraningList = self.allTraningDataSource[indexPath.row]
        
        cell.haderImageView.kf.setImage(with: URL(string: allTraningList?.previewUrl ?? ""))
    
        cell.titleInfoLabel.text = allTraningList?.zhName ?? ""
        
        return cell
    }
    
    
}
